package com.whale.cloud.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * Created by maoweidong on 16/6/16.
 */

public class JSONArraySortUtil {

    public static JSONArray sortDesc(JSONArray jsonArray, final String key) {

        Collections.sort(jsonArray, new Comparator<Object>() {
            private String KEY_NAME = key;

            @Override
            public int compare(Object a, Object b) {
                String valA = new String();
                String valB = new String();

                try {
                    valA = ((JSONObject) a).getString(KEY_NAME);
                    valB = ((JSONObject) b).getString(KEY_NAME);
                } catch (JSONException e) {
                    //do something
                }

                return valB.compareTo(valA);
                //if you want to change the sort order, simply use the following:
                //return -valA.compareTo(valB);
            }
        });

        return jsonArray;
    }

    public static JSONArray sortAesc(JSONArray jsonArray, final String key) {

        Collections.sort(jsonArray, new Comparator<Object>() {
            private String KEY_NAME = key;

            @Override
            public int compare(Object a, Object b) {
                String valA = new String();
                String valB = new String();

                try {
                    valA = ((JSONObject) a).getString(KEY_NAME);
                    valB = ((JSONObject) b).getString(KEY_NAME);
                } catch (JSONException e) {
                    //do something
                }

                return valA.compareTo(valB);
                //if you want to change the sort order, simply use the following:
                //return -valA.compareTo(valB);
            }
        });

        return jsonArray;
    }

    public static JSONArray sortDesc(JSONArray jsonArray, final String key, String type) {

        JSONArray sortedJsonArray = new JSONArray();

        JSONArray jsonArr = JSONArray.parseArray(jsonArray.toJSONString());

        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(jsonArr.getJSONObject(i));
        }
        Collections.sort(jsonValues, new Comparator<JSONObject>() {
            private String KEY_NAME = key;

            @Override
            public int compare(JSONObject a, JSONObject b) {
                Double valA = 0d;
                Double valB = 0d;

                try {
                    valA = a.getDouble(KEY_NAME);
                    valB = b.getDouble(KEY_NAME);
                } catch (JSONException e) {
                    //do something
                }

                return valB.compareTo(valA);
                //if you want to change the sort order, simply use the following:
                //return -valA.compareTo(valB);
            }
        });

        for (int i = 0; i < jsonArr.size(); i++) {
            sortedJsonArray.add(jsonValues.get(i));
        }
        return sortedJsonArray;
    }

    public static JSONArray jsonArraysortTwo(JSONArray jsonArray, final String key, String type) {

        JSONArray sortedJsonArray = new JSONArray();

        JSONArray jsonArr = JSONArray.parseArray(jsonArray.toJSONString());

        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(jsonArr.getJSONObject(i));
        }
        Collections.sort(jsonValues, new Comparator<JSONObject>() {
            private String KEY_NAME = key;

            @Override
            public int compare(JSONObject a, JSONObject b) {
                Double valA = 0d;
                Double valB = 0d;

                try {
                    String vala = a.getString(KEY_NAME);
                    if (vala.contains("以上")) {
                        vala = vala.substring(0, vala.indexOf("以"));
                    } else {
                        vala = vala.substring(0, vala.indexOf("-"));
                    }
                    String valb = b.getString(KEY_NAME);
                    if (valb.contains("以上")) {
                        valb = valb.substring(0, valb.indexOf("以"));
                    } else {
                        valb = valb.substring(0, valb.indexOf("-"));
                    }
                    valA = Double.valueOf(vala);
                    valB = Double.valueOf(valb);
                } catch (JSONException e) {
                    //do something
                }

                return valB.compareTo(valA);
                //if you want to change the sort order, simply use the following:
                //return -valA.compareTo(valB);
            }
        });

        for (int i = 0; i < jsonArr.size(); i++) {
            sortedJsonArray.add(jsonValues.get(i));
        }
        return sortedJsonArray;
    }

    public static int bisectionSearch(int value, int[] array) {
        int minIndex = 0;
        int curIndex = 0;
        int maxIndex = array.length - 1;
        int count = 0;  // 用于循环次数记数
        int result = -1;

        if (value == array[maxIndex]) {
            result = maxIndex;
        } else if (value == array[minIndex]) {
            result = minIndex;
        } else {
            while (true) {
                // 如果两者相同或相邻，由于二者的值（除首次）均来自于已与value比较不等的值，因此数组中没有该值。
                if (2 > maxIndex - minIndex) {
                    result = -1;
                    break;
                }
                curIndex = minIndex + (maxIndex - minIndex) / 2;
                if (array[curIndex] == value) {
                    result = curIndex;
                    break;
                } else if (array[curIndex] < value) {
                    minIndex = curIndex;
                } else {
                    maxIndex = curIndex;
                }
                count++;
            }
        }
        System.out.println("loops = " + count);
        return result;
    }

}
